package org.jeecg.modules.consumptionFuel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.consumptionFuel.entity.ConsumptionFuel;
import org.jeecg.modules.consumptionFuel.entity.FuelSelectDTO;
import org.jeecg.modules.consumptionFuel.mapper.ConsumptionFuelMapper;
import org.jeecg.modules.consumptionFuel.service.IConsumptionFuelService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 醇基燃料能耗
 * @Author: jeecg-boot
 * @Date: 2023-06-20
 * @Version: V1.0
 */
@Service
public class ConsumptionFuelServiceImpl extends ServiceImpl<ConsumptionFuelMapper, ConsumptionFuel> implements IConsumptionFuelService {

    @Override
    public List<ConsumptionFuel> selectByTimeAndDept(FuelSelectDTO selectDTO) {
        Date start = Date.from(selectDTO.getStartTime().toInstant());
        Date end = Date.from(selectDTO.getEndTime().toInstant());
        String paramS = DateUtils.formatDate(start, "yyyyMMdd");
        String paramE = DateUtils.formatDate(end, "yyyyMMdd");
        int s = Integer.parseInt(paramS);
        int e = Integer.parseInt(paramE);
        if (selectDTO.getStartTime() == null || selectDTO.getEndTime() == null) {
            throw new JeecgBootException("服务器忙,请稍后再试...");
        } else {
            if ("date".equals(selectDTO.getDatePickerType())) {
                LambdaQueryWrapper<ConsumptionFuel> wrapper = Wrappers.lambdaQuery();
                wrapper.between(ConsumptionFuel::getDate, s, e).and(wrapper1 -> {
                    wrapper1.eq(ConsumptionFuel::getDept, selectDTO.getDept());
                }).orderByAsc(ConsumptionFuel::getDate);
                return baseMapper.selectList(wrapper);
            } else {
                QueryWrapper<ConsumptionFuel> wrapper = new QueryWrapper<>();
                wrapper.select("sum(consumption) as consumption", "year", "month")
                        .groupBy("month", "year").having("consumption > 0").orderByAsc("year*1", "month*1");
                return baseMapper.selectList(wrapper);
            }
        }
    }

}
